echowithcolor(COLOR GREEN "-- Configure libcopp")

# ========== build library ==========
source_group_by_dir(COPP_SRC_LIST)
# source_group_by_dir(COPP_OBJ_LIST)

if(LIBCOPP_FCONTEXT_USE_TSX)
  add_compiler_define(LIBCOPP_FCONTEXT_USE_TSX=1)
endif()
if(LIBCOPP_MACRO_TLS_STACK_PROTECTOR)
  add_compiler_define(LIBCOPP_FCONTEXT_TLS_STACK_PROTECTOR=1)
endif()

if(BUILD_SHARED_LIBS OR LIBCOPP_USE_DYNAMIC_LIBRARY)
  add_library(${PROJECT_LIBCOPP_LIB_LINK} SHARED ${COPP_SRC_LIST} ${COPP_OBJ_LIST})
  set_target_properties(
    ${PROJECT_LIBCOPP_LIB_LINK}
    PROPERTIES C_VISIBILITY_PRESET "hidden"
               CXX_VISIBILITY_PRESET "hidden"
               VERSION ${PROJECT_VERSION}
               SOVERSION ${PROJECT_VERSION}
               INTERFACE_COMPILE_DEFINITIONS LIBCOPP_API_DLL=1)
  target_compile_definitions(${PROJECT_LIBCOPP_LIB_LINK} PRIVATE LIBCOPP_API_COPP_NATIVE=1 LIBCOPP_API_DLL=1)
else()
  add_library(${PROJECT_LIBCOPP_LIB_LINK} STATIC ${COPP_SRC_LIST} ${COPP_OBJ_LIST})
  set_target_properties(
    ${PROJECT_LIBCOPP_LIB_LINK}
    PROPERTIES C_VISIBILITY_PRESET "hidden"
               CXX_VISIBILITY_PRESET "hidden"
               VERSION ${PROJECT_VERSION})
  target_compile_definitions(${PROJECT_LIBCOPP_LIB_LINK} PRIVATE LIBCOPP_API_COPP_NATIVE=1)
endif()

target_include_directories(
  ${PROJECT_LIBCOPP_LIB_LINK}
  INTERFACE "$<INSTALL_INTERFACE:include>"
  PUBLIC "$<BUILD_INTERFACE:${PROJECT_LIBCOPP_ROOT_INC_DIR}>" "$<BUILD_INTERFACE:${LIBCOPP_GENERATED_DIR}/include>")
if(PROJECT_LIBCOPP_DEP_INC_DIR)
  target_include_directories(${PROJECT_LIBCOPP_LIB_LINK} PUBLIC "$<BUILD_INTERFACE:${PROJECT_LIBCOPP_DEP_INC_DIR}>")
endif()

if(LIBCOPP_MACRO_ENABLE_STD_COROUTINE)
  add_target_properties(${PROJECT_LIBCOPP_LIB_LINK} INTERFACE_COMPILE_FEATURES cxx_std_20)
  if(COMPILER_OPTIONS_STD_COROUTINE_FLAGS)
    target_compile_options(${PROJECT_LIBCOPP_LIB_LINK}
                           INTERFACE "$<$<COMPILE_LANGUAGE:CXX>:${COMPILER_OPTIONS_STD_COROUTINE_FLAGS}>")
  endif()
endif()

add_library(libcopp::${PROJECT_LIBCOPP_LIB_LINK} ALIAS ${PROJECT_LIBCOPP_LIB_LINK})
set_property(TARGET ${PROJECT_LIBCOPP_LIB_LINK} PROPERTY FOLDER "libcopp")

install(
  TARGETS ${PROJECT_LIBCOPP_LIB_LINK}
  EXPORT ${PROJECT_LIBCOPP_EXPORT_NAME}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(
  DIRECTORY "${PROJECT_LIBCOPP_INC_DIR}"
  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
  USE_SOURCE_PERMISSIONS
  PATTERN ".svn" EXCLUDE
  PATTERN ".git" EXCLUDE
  PATTERN "*.in" EXCLUDE)

install(FILES "${LIBCOPP_GENERATED_DIR}/include/libcopp/utils/config/libcopp_build_features.h"
        DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libcopp/utils/config/")

export(
  TARGETS ${PROJECT_LIBCOPP_LIB_LINK}
  FILE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/cmake/libcopp/${PROJECT_LIBCOPP_EXPORT_NAME}.cmake"
  NAMESPACE libcopp::
  EXPORT_LINK_INTERFACE_LIBRARIES)
install(
  EXPORT ${PROJECT_LIBCOPP_EXPORT_NAME}
  NAMESPACE "libcopp::"
  DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/libcopp")
